package com.jk.customer.controller;

import com.jk.customer.entity.MaterialsEntity;
import com.jk.customer.entity.PictureEntity;
import com.jk.customer.entity.PictureFile;
import com.jk.customer.entity.ProductEntity;
import com.jk.customer.service.PurchaseService;
import com.jk.customer.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    /**
     *
     * @param product
     * @return
     * @throws ParseException
     */
    @RequestMapping("getPicture")
    @ResponseBody
    public List<ProductEntity> getPicture(ProductEntity product , HttpServletRequest request) throws ParseException {

        List<ProductEntity> list = purchaseService.getPicture(product,request);

         return list;
    }

    @RequestMapping("upload_cyt")
    @ResponseBody
    public Object upload(MultipartFile accessory) throws Exception {

        HashMap<Object, Object> hashMap = new HashMap<>();
        String oss = OSSUtil.upload2oss(accessory);

        hashMap.put("accessory",oss);

        return hashMap;
    }

    /**
     * 新增主表方法
     * @param product
     * 蔡运通
     */
    @RequestMapping("addPicture_cyt")
    @ResponseBody
    public void addPicture_cyt(ProductEntity product,HttpServletRequest request){

        purchaseService.addPicture_cyt(product,request);
    }

    /**
     * 寻图图片上传
     * @param contactsAccessory
     * @return hashMap
     * @throws Exception
     * 修改人---蔡运通
     */
    @RequestMapping("addPictureImg_cyt")
    @ResponseBody
    public Object addPictureImg_cyt(MultipartFile contactsAccessory) throws Exception {

        String upload2oss = OSSUtil.upload2oss(contactsAccessory);

        HashMap<Object, Object> hashMap = new HashMap<>();

        hashMap.put("contactsAccessory",upload2oss);

        return hashMap;
    }

    /**
     * 新增寻图表
     * @param picture
     * 修改人：蔡运通
     */
    @RequestMapping("addPicturefindImg_cyt")
    @ResponseBody
    public void addPicturefindImg_cyt(PictureEntity picture,HttpServletRequest request) throws ParseException {

        System.err.println(picture.getQueryPictureStartDate());

        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String format = simpleDateFormat.format(date);
        System.out.println(format);


        if (picture.getQueryPictureStartDate().equals(",")){
            picture.setQueryPictureStartDate(format);
            picture.setQueryPictureState(5);
        }

        purchaseService.addPicturefindImg_cyt(picture,request);
    }

    /**
     * 删除寻图列表
     * @param productId
     * 修改人：蔡运通
     */
    @RequestMapping("delById_cyt")
    @ResponseBody
    public void delById_cyt(String productId){

        purchaseService.delById_cyt(productId);
    }

    /**
     * 取消寻图方法
     * @param productId
     * 修改人：蔡运通
     */
    @RequestMapping("cancelById_cyt")
    @ResponseBody
    public Map<String,Object> cancelById_cyt(String productId){

        purchaseService.cancelById_cyt(productId);
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("message","成功");
        return objectObjectHashMap;
    }

    /**
     * 方法描述：重新发起寻图单
     * @param productId
     * 修改人：蔡运通
     */
    @RequestMapping("toInitiatePicture_cyt")
    @ResponseBody
    public void toInitiatePicture_cyt(String productId){

        purchaseService.toInitiatePicture_cyt(productId);
    }

    /**
     *
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping("upUpdatePicture_cyt")
    public String upUpdatePicture_cyt(String productId, Model model){

        ProductEntity productEntity = new ProductEntity();

        productEntity.setProductId(productId);

        model.addAttribute("productId",productEntity);

        return "findCustomized";
    }

    /**
     *
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping("updatePictureById_cyt")
    public String updatePictureById_cyt(String productId, Model model){

        ProductEntity productEntity = new ProductEntity();

        productEntity.setProductId(productId);

        model.addAttribute("productId",productEntity);

        return "addPicture";
    }


    /**
     * 寻图回显
     * @param productId
     * @return
     * 修改人：蔡运通
     */
    @RequestMapping("selById_cyt")
    @ResponseBody
    public ProductEntity selById_cyt(String productId){

        return purchaseService.selById_cyt(productId);
    }

    /**
     * 方法描述：寻图单回显
     * @param productId
     * @return
     * 修改人：蔡运通
     */
    @RequestMapping("selByPictureId_cyt")
    @ResponseBody
    public PictureEntity selByPictureId_cyt(String productId){

        return purchaseService.selByPictureId_cyt(productId);
    }

    /**
     * 方法描述（新增材料表）
     * @param materialsEntity
     * 修改人：蔡运通
     */
    @RequestMapping("addmaterials_cyt")
    @ResponseBody
    public void addmaterials_cyt(MaterialsEntity materialsEntity,HttpServletRequest request){

        purchaseService.addmaterials_cyt(materialsEntity,request);
    }

    /**
     * 方法描述（延期回显）
     * @param productId
     * @return
     * 修改人：蔡运通
     */
    @RequestMapping("selYanQiById")
    @ResponseBody
    public PictureEntity selYanQiById(String productId){

        return purchaseService.selYanQiById(productId);
    }

    /**
     * 方法描述（延期）
     * @param product
     * 修改人：蔡运通
     */
    @RequestMapping("updateYanQi")
    @ResponseBody
    public void updateYanQi(ProductEntity product){

        purchaseService.updateYanQi(product);
    }

    /**
     * 方法描述（我要比价）
     * @param id
     * @return
     * 修改人：蔡运通
     */
    @RequestMapping("getUserByTypeOne")
    @ResponseBody
    public List<MaterialsEntity> getUserByTypeOne(String id){

        return purchaseService.getuserByTypeOne(id);
    }

    /**
     * 方法描述（查看存Id）
     * @param productId
     * @param model
     * @return
     * 修改人：蔡运通
     */
    @RequestMapping("findImgData")
    public String findImgData(String productId, Model model){
        ProductEntity productEntity = new ProductEntity();

        productEntity.setProductId(productId);

        model.addAttribute("productId",productEntity);

        return "findImgData";
    }

    /**
     * 方法描述（查看）
     * @param productId
     * @return
     * 修改人：蔡运通
     */
    @RequestMapping("findImgData_cyt")
    @ResponseBody
    public List<PictureFile> findImgData_cyt(String productId){

        return purchaseService.findImgData_cyt(productId);
    }

    /**
     * 方法描述（查看材料存Id）
     * @param commonId
     * @param model
     * @return materials
     * 修改人：蔡运通
     */
    @RequestMapping("toFindMaterials")
    public String toFindMaterials(String commonId,String userRealName , Model model){
        PictureFile pictureFile = new PictureFile();

        pictureFile.setCommonId(commonId);

        pictureFile.setPictureName(userRealName);

        model.addAttribute("commonId",pictureFile);

        return "materials";
    }

    /**
     * 方法描述（查询材料表）
     * @param commonId
     * @return list
     * 修改人：蔡运通
     */
    @RequestMapping("findMaterials_cyt")
    @ResponseBody
    public List<ProductEntity> findMaterials_cyt(String commonId,String userRealName){

        return purchaseService.findMaterials_cyt(commonId, userRealName);
    }

    /**
     * 方法描述（定图）
     * @param queryPictureId
     * 修改人：蔡运通
     */
    @RequestMapping("confirmFindImg_cyt")
    @ResponseBody
    public void confirmFindImg_cyt(String queryPictureId,String userRealName,String commonQueryId){

        purchaseService.confirmFindImg_cyt(queryPictureId,userRealName,commonQueryId);
    }


    /**
     * 方法描述（查看详情存Id）
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping("findParticulars")
    public String findParticulars(String productId, Model model){

        ProductEntity productEntity = new ProductEntity();

        productEntity.setProductId(productId);

        model.addAttribute("productId",productEntity);

        return "findParticulars";
    }

    /**
     * 方法描述（查看详情）
     * @param productId
     * @return
     * 修改人：蔡运通
     */
    @RequestMapping("findParticularsData")
    @ResponseBody
    public List<ProductEntity> findParticularsData(String productId){

        return purchaseService.findParticularsData(productId);
    }

    /**
     * 方法描述（查询我的材料单）
     * @return list
     * 修改人：蔡运通
     */
    @RequestMapping("findBillOfMaterialsData")
    @ResponseBody
    public List<MaterialsEntity> findBillOfMaterialsData(HttpServletRequest request){

        List<MaterialsEntity> list = purchaseService.findBillOfMaterialsData(request);

        return list;
    }

    /**
     * 方法描述（查询总页面）
     * @return list
     * 修改人：蔡运通
     */
    @RequestMapping("findTotalPages")
    @ResponseBody
    public List<ProductEntity> findTotalPages(HttpServletRequest request){

        List<ProductEntity> list = purchaseService.findTotalPages(request);

        return list;
    }

    /**
     * 方法描述（所有材料单查询）
     * @return list
     * 修改人：蔡运通
     */
    @RequestMapping("total_materials")
    @ResponseBody
    public List<ProductEntity> total_materials(){

        return purchaseService.total_materials();
    }

}